package xyz.xtt.equity.controller;

import static xyz.xtt.common.asset.AssertUtils.isFalse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.equity.constants.ErrorMessage;
import xyz.xtt.equity.service.ITblAssetEntitlementDeliverService;

/**
 * @author dale
 * @date 2025/3/4
 **/
@Tag(name = "权益管理", description = "权益管理")
@RefreshScope
@Slf4j
@RequestMapping("/api/nft/equity")
@RestController
public class DeliverController {
	@Autowired
	private ITblAssetEntitlementDeliverService iTblAssetEntitlementDeliverService;

	/**
	 * 原 nft-exchange
	 * xyz.xtt.exchange.controller.InterestController.exchangeWork(Long, Long,
	 * Integer, Long) 方法
	 * 
	 * @param uid
	 * @param id
	 * @param gender
	 * @param assetId
	 * @return
	 */
	@Operation(summary = "365用户兑换权益", description = "365用户兑换权益")
	@GetMapping("/exchangeWork")
	public ApiResult<String> exchangeWork(@RequestHeader("uid") Long uid, @RequestParam(value = "id") Long id,
			@RequestParam(value = "gender", required = false) Integer gender, @RequestParam(value = "assetId") Long assetId) {
		log.info("/api/nft/equity/exchangeWork uid:{},assetId: {},entitlementId:{}", uid, assetId, id);
		Boolean exchangeWorkEx = iTblAssetEntitlementDeliverService.exchangeWorkEx(uid, id, gender, assetId);
		log.info("/api/nft/equity/exchangeWork uid:{},assetId: {},entitlementId:{} result:{}", uid, assetId, id, exchangeWorkEx);
		isFalse(exchangeWorkEx, ErrorMessage.EQUITY_EXCHANGE_FAILD);
		return ApiResult.ok("兑换成功");
	}

	@Operation(summary = "365用户兑换检查", description = "365用户兑换检查")
	@GetMapping("/deliverCheck")
	public ApiResult<Boolean> deliverCheck(Long entitlementId, Long uid) {
		log.info("/api/exchange/interest/deliverCheck uid:{},entitlementId:{}", uid, entitlementId);
		boolean checkDeliverSucc = iTblAssetEntitlementDeliverService.checkDeliverSucc(entitlementId, uid);
		return ApiResult.ok(checkDeliverSucc);
	}
}
